package com.yuandian.ops.workbench.controller;


import com.yuandian.api.business_monitor.feign.RemoteAlarmMergingQueryService;
import com.yuandian.api.business_monitor.feign.RemoteBaseAlarmService;
import com.yuandian.api.business_monitor.po.AlarmParams;
import com.yuandian.api.business_monitor.po.AlarmReverseAnalysisParams;
import com.yuandian.api.business_monitor.vo.AlarmProportionVo;
import com.yuandian.api.business_monitor.vo.BaseBusinessAlarmVo;
import com.yuandian.api.business_monitor.vo.BusinessAlarmDetailVo;
import com.yuandian.api.business_monitor.vo.BusinessAlarmStepVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.utils.PageDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 告警业务查询
 * </p>
 *
 * @author fucx
 * @since 2020-11-10
 */
@RestController
@RequestMapping("/alarm/business")
@Api(tags = "告警业务查询接口")
@Slf4j
public class BusiAlarmController {

	@Autowired
	private RemoteAlarmMergingQueryService busiAlarmService;

	@Autowired
	private RemoteBaseAlarmService baseAlarmService;

	
	/**
	 * 按照系统ID、业务编码及时间范围分类统计各类型告警(环形图)
	 * @param params  查询参数
	 * @return 返回通用结果集
	 */
	@ApiOperation(value = "基础告警归并分析-告警类型占比", notes = "基础告警归并分析-告警类型占比")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", required = true,   dataType = "Integer"),
			@ApiImplicitParam(name = "alarmStates",  		value = "告警分类",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "alarmTypeId",  		value = "告警类型",  		paramType = "query", required = false,   dataType = "Integer"),
			@ApiImplicitParam(name = "businessCodes", 		value = "业务编码",  		paramType = "query", required = false,  dataType = "List<String>"),
			@ApiImplicitParam(name = "segmentIds", 			value = "网端ID列表",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 			paramType = "query", required = true,   dataType = "String")
	})
	@PostMapping("/getBusinessAlarmPercent")
	public R<List<AlarmProportionVo>> getBusinessAlarmPercent(@RequestBody AlarmParams params) {
		R<List<AlarmProportionVo>> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = busiAlarmService.getBusinessAlarmPercent(params);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-告警类型占比]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-告警类型占比]-报错: {}" + e.getMessage());
		}
		return result;
	}


	/**
	 * 按照系统ID、业务编码及时间范围分类统计各等级告警(环形图)
	 * @param params  查询参数
	 * @return 返回通用结果集
	 */
	@ApiOperation(value = "基础告警归并分析-告警等级占比", notes = "基础告警归并分析-告警等级占比")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", required = true,   dataType = "Integer"),
			@ApiImplicitParam(name = "alarmStates",  		value = "告警分类",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "alarmTypeId",  		value = "告警类型",  		paramType = "query", required = false,   dataType = "Integer"),
			@ApiImplicitParam(name = "businessCodes", 		value = "业务编码",  		paramType = "query", required = false,  dataType = "List<String>"),
			@ApiImplicitParam(name = "segmentIds", 			value = "网端ID列表",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 			paramType = "query", required = true,   dataType = "String")
	})
	@PostMapping("/getBusinessAlarmLevelPercent")
	public R<List<AlarmProportionVo>> getBusinessAlarmLevelPercent(@RequestBody AlarmParams params) {

		R<List<AlarmProportionVo>> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = busiAlarmService.getBusinessAlarmLevelPercent(params);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-告警等级占比]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-告警等级占比]-报错: {}" + e.getMessage());
		}

		return result;
	}


	/**
	 * 按照系统ID、业务编码、时间范围、端口ID查询各告警对应 不同等级 的告警总数列表
	 * @param params  查询参数（系统ID、业务编码、时间范围、端口ID）
	 * @return 返回通用结果集
	 */
	@ApiOperation(value = "基础告警归并分析-不同等级告警统计列表", notes = "基础告警归并分析-不同等级告警统计列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", required = true,   dataType = "Integer"),
			@ApiImplicitParam(name = "alarmStates",  		value = "告警分类",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "businessCodes", 		value = "业务编码",  		paramType = "query", required = false,  dataType = "List<String>"),
			@ApiImplicitParam(name = "segmentIds", 			value = "网端ID列表",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 		value = "排序字段", 			paramType = "query", required = false,  dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 				value = "升降序:ASC，DESC",  paramType = "query",  required = false, dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 			paramType = "query", required = true,   dataType = "String")
	})
	@PostMapping("/getBusinessGradeDate")
	public R<PageDataInfo> getBusinessGradeDate(@RequestBody AlarmParams params) {
		R<PageDataInfo> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = busiAlarmService.getBusinessGradeDate(params);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-不同等级告警统计列表]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-不同等级告警统计列表]-报错: {}" + e.getMessage());
		}
		return result;
	}


	/**
	 * 按照系统ID、业务编码、时间范围、端口ID和告警类型id查询各告警详情列表
	 * @param params  查询参数（系统ID、业务编码、时间范围、端口ID）
	 * @return 返回通用结果集
	 */
	@ApiOperation(value = "基础告警归并分析-告警详情列表", notes = "基础告警归并分析-告警详情列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", required = true,   dataType = "Integer"),
			@ApiImplicitParam(name = "alarmStates",  		value = "告警分类",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "alarmTypeId",  		value = "告警类型",  		paramType = "query", required = false,   dataType = "Integer"),
			@ApiImplicitParam(name = "businessCodes", 		value = "业务编码",  		paramType = "query", required = false,  dataType = "List<String>"),
			@ApiImplicitParam(name = "segmentIds", 			value = "网端ID列表",  		paramType = "query", required = false,  dataType = "List<Integer>"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", required = true,   dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 		value = "排序字段", 			paramType = "query", required = false,  dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 				value = "升降序:ASC，DESC",  paramType = "query",  required = false, dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 			paramType = "query", required = true,   dataType = "String")
	})
	@PostMapping("/getBusinessAlarmList")
	public R<PageDataInfo> getBusinessAlarmList(@RequestBody AlarmParams params) {
		R<PageDataInfo> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = busiAlarmService.getBusinessAlarmList(params);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-告警详情列表]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警归并分析-告警详情列表]-报错: {}" + e.getMessage());
		}
		return result;
	}


	/**
	 * 基础告警列表查询
	 * @param params  查询参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "基础告警列表", notes = "基础告警列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 			value = "系统ID",  				paramType = "query", required = false,   dataType = "Integer"),
			@ApiImplicitParam(name = "businessCodes", 		value = "业务编码列表",  			paramType = "query", required = false,   dataType = "List<String>"),
			@ApiImplicitParam(name = "stepCodes", 			value = "告警步骤编码列表",  		paramType = "query", required = false,   dataType = "List<String>"),
			@ApiImplicitParam(name = "alarmIndicatorIds",  	value = "告警指标id列表",  		paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "alarmLevelIds",  		value = "告警级别id",  			paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "alarmStates",  		value = "告警分类",  			paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "category", 			value = "告警类别 BPM APM",  	paramType = "query", required = true,    dataType = "String"),
			@ApiImplicitParam(name = "headerFlag", 			value = "是否首端标志：1是，0否",  	paramType = "query", required = true,    dataType = "Integer"),
			@ApiImplicitParam(name = "relvanceDataId", 		value = "基础告警的数据id",  		paramType = "query", required = false,   dataType = "String"),
			@ApiImplicitParam(name = "segmentIds", 			value = "网端ID列表",  			paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 					paramType = "query", required = true,    dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 				paramType = "query", required = true,    dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 		value = "排序字段", 				paramType = "query", required = false,   dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 				value = "升降序:ASC，DESC",  	paramType = "query",  required = false,  dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 				paramType = "query", required = true,    dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 				paramType = "query", required = true,    dataType = "String")
	})
	@PostMapping("/getBaseAlarmList")
	public R<PageDataInfo> getBaseAlarmList(@RequestBody AlarmParams params) {

		R<PageDataInfo> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = baseAlarmService.getBaseAlarmList(params);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警列表查询]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警列表查询]-报错: {}" + e.getMessage());
		}
		return result;
	}


	/**
	 * 基础告警列表查询-下载
	 * @param params  查询参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "基础告警列表-下载", notes = "基础告警列表-下载")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", 					value = "系统ID",  				paramType = "query", required = false,   dataType = "Integer"),
			@ApiImplicitParam(name = "systemName", 					value = "系统名称",  			paramType = "query", required = false,   dataType = "Integer"),
			@ApiImplicitParam(name = "businessCodes", 				value = "业务编码列表",  			paramType = "query", required = false,   dataType = "List<String>"),
			@ApiImplicitParam(name = "stepCodes", 					value = "告警步骤编码列表",  		paramType = "query", required = false,   dataType = "List<String>"),
			@ApiImplicitParam(name = "alarmIndicatorIds",  			value = "告警指标id列表",  		paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "alarmLevelIds",  				value = "告警级别id",  			paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "alarmStates",  				value = "告警分类",  			paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "displayFields",  				value = "展示字段",  			paramType = "query", required = true,   dataType = "List<String>"),
			@ApiImplicitParam(name = "displayMappingFields",  		value = "展示字段英文",  			paramType = "query", required = true,   dataType = "List<String>"),
			@ApiImplicitParam(name = "category", 					value = "告警类别 BPM APM",  	paramType = "query", required = true,    dataType = "String"),
			@ApiImplicitParam(name = "headerFlag", 					value = "是否首端标志：1是，0否",  	paramType = "query", required = true,    dataType = "Integer"),
			@ApiImplicitParam(name = "relvanceDataId", 				value = "基础告警的数据id",  		paramType = "query", required = false,   dataType = "String"),
			@ApiImplicitParam(name = "segmentIds", 					value = "网端ID列表",  			paramType = "query", required = false,   dataType = "List<Integer>"),
			@ApiImplicitParam(name = "orderByColumn", 				value = "排序字段", 				paramType = "query", required = false,   dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 						value = "升降序:ASC，DESC",  	paramType = "query",  required = false,  dataType = "String"),
			@ApiImplicitParam(name = "startTime", 					value = "开始时间", 				paramType = "query", required = true,    dataType = "String"),
			@ApiImplicitParam(name = "endTime",   					value = "结束时间", 				paramType = "query", required = true,    dataType = "String")
	})
	@PostMapping("/exportBaseAlarmExcel")
	public R<Object> exportBaseAlarmExcel(@RequestBody AlarmParams params) {
		// 设置默认排序
		if (params.getOrderByColumn() == null || ("").equals(params.getOrderByColumn())) {
			params.setOrderByColumn("alarmTriggerTime");
			params.setIsAsc("desc");
		}

		try {
			long sTime = System.currentTimeMillis();
			baseAlarmService.exportBaseAlarmExcel(params);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警列表查询-下载]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警列表查询-下载]-报错: {}" + e.getMessage());
		}
		return R.ok();
	}


	/**
	 * 基础告警-详情-告警总览
	 *
	 * @param id	告警数据主键ID
	 * @param startTime	开始时间
	 * @param endTime	结束时间
	 * @return	通用结果集
	 */
	@ApiOperation(value = "基础告警-详情-告警总览", notes = "基础告警-详情-告警总览")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", 			value = "告警数据主键ID",  	paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "startTime", 	value = "开始时间", 			paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   	value = "结束时间", 			paramType = "query",  required = true,   dataType = "String")
	})
	@GetMapping("/getBusinessAlarmDetail")
	public R<BaseBusinessAlarmVo> getBusinessAlarmDetail(String id, String startTime, String endTime) {
		R<BaseBusinessAlarmVo> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = baseAlarmService.getBusinessAlarmDetail(id, startTime, endTime);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警-详情-告警总览]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警-详情-告警总览]-报错: {}" + e.getMessage());
		}
		return result;
	}


	/**
	 * 基础告警-详情-业务详情回溯信息
	 *
	 * @param id	告警数据主键ID
	 * @param startTime	开始时间
	 * @param endTime	结束时间
	 * @return	通用结果集
	 */
	@ApiOperation(value = "基础告警-详情-业务详情回溯信息", notes = "基础告警-详情-业务详情回溯信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", 			value = "告警数据主键ID",  	paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "startTime", 	value = "开始时间", 			paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   	value = "结束时间", 			paramType = "query",  required = true,   dataType = "String")
	})
	@GetMapping("/getBusinessDetailList")
	public R<List<BusinessAlarmDetailVo>> getBusinessDetailList(String id, String startTime, String endTime) {
		R<List<BusinessAlarmDetailVo>> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = baseAlarmService.getBusinessDetailList(id, startTime, endTime);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警-详情-业务详情回溯信息]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警-详情-业务详情回溯信息]-报错: {}" + e.getMessage());
		}

		return result;
	}


	/**
	 * 基础告警-详情-业务步骤信息
	 *
	 * @param id	告警数据主键ID
	 * @param startTime	开始时间
	 * @param endTime	结束时间
	 * @return	通用结果集
	 */
	@ApiOperation(value = "基础告警-详情-业务步骤信息", notes = "基础告警-详情-业务步骤信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", 			value = "告警数据主键ID",  	paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "startTime", 	value = "开始时间", 			paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   	value = "结束时间", 			paramType = "query",  required = true,   dataType = "String")
	})
	@GetMapping("/getStepDetailList")
	public R<List<BusinessAlarmStepVo>> getStepDetailList(String id, String startTime, String endTime) {
		R<List<BusinessAlarmStepVo>> stepDetailList = null;
		try {
			long sTime = System.currentTimeMillis();
			stepDetailList = baseAlarmService.getStepDetailList(id, startTime, endTime);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[基础告警-详情-业务步骤信息]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[基础告警-详情-业务步骤信息]-报错: {}" + e.getMessage());
		}

		return stepDetailList;
	}

	/**
	 * 告警反推分析
	 * @param params 告警反推分析请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "基础告警-详情-告警反推分析", notes = "基础告警-详情-告警反推分析")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", 			value = "告警数据主键ID",  	paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "startTime", 	value = "开始时间", 			paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime", 	value = "结束时间", 			paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "startTime", 	value = "开始时间", 			paramType = "query",  required = true,   dataType = "String"),
			@ApiImplicitParam(name = "endTime",   	value = "结束时间", 			paramType = "query",  required = true,   dataType = "String")
	})
	@PostMapping("/reverseAnalysis")
	public R<Map<String, Object>> reverseAnalysis(@RequestBody AlarmReverseAnalysisParams params) {
		Map<String, Object> map = null;
//		map = baseAlarmService.reverseAnalysis(params);	TODO 远程调用
		return R.ok(map);
	}

}
